//
//  MarkListViewModel.m
//  AppleMarket
//
//  Created by yangy on 30/10/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "MarkListViewModel.h"
#import "MarketObject.h"

@implementation MarkListViewModel
- (instancetype)init
{
    self = [super init];
    if(self){
        
        [[kNotificationCenter rac_addObserverForName:kReceiveFullMarketNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {
            NSArray<MarketObject*> *markets = [x.object copy];
            NSArray<MarketObject*> *sortMarkets = [NSArray array];
            sortMarkets = [markets sortedArrayUsingComparator:^NSComparisonResult(MarketObject  *obj1, MarketObject  *obj2) {
                if([obj1.DailyChangePerc floatValue] > [obj2.DailyChangePerc floatValue]){
                    return NSOrderedAscending; //
                }else if([obj1.DailyChangePerc floatValue] < [obj2.DailyChangePerc floatValue]){
                    return NSOrderedDescending; // 降序
                }else{
                    return NSOrderedSame;
                }
            }];
            
            if(markets.count > 8){
                self.dataList = [sortMarkets subarrayWithRange:NSMakeRange(0, 8)];
            }else{
                self.dataList = [sortMarkets copy];
            }
            [self.refershMarketEndSubject sendNext:nil];
        }];
        
    }
    return self;
}

- (RACSubject *)cellClickSubject
{
    if(!_cellClickSubject){
        _cellClickSubject = [RACSubject subject];
    }
    return _cellClickSubject;
}

- (RACSubject *)refershMarketEndSubject
{
    if(!_refershMarketEndSubject){
        _refershMarketEndSubject = [RACSubject subject];
    }
    return _refershMarketEndSubject;
}

@end
